<template>
  <static-content-wrapper
    :designer="designer"
    :settings="settings"
    :field="field"
    :design-state="designState"
    :hidden="field.options.hidden"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId"
  >
    <div class="field-editor slider-field-editor">
      <el-slider
        ref="fieldEditor"
        v-model="field.options.pos"
        :min="field.options.minrange"
        :max="field.options.maxrange"
        :step="field.options.step"
        :style="style"
        :show-tooltip="false"
        disabled
      >
      </el-slider>
    </div>
  </static-content-wrapper>
</template>

<script>
import emitter from '@/mixins/emitter'
import fieldMixin from '@/mixins/fieldMixin'

export default {
  name: 'SliderWidget',
  componentName: 'FieldWidget',
  // 必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    settings: Object,
    scale: {
      type: Number,
      default: 1
    },

    designState: {
      type: Boolean,
      default: false
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: ''
    }
  },
  computed: {
    scaleWidth() {
      return this.field.options.width * this.scale
    },
    scaleHeight() {
      return this.field.options.height * this.scale
    },
    percentage() {
      const selected = this.field.options.pos || 0
      const maxRange = this.field.options.maxrange || 100
      return Math.round((selected / maxRange.toFixed(2)) * 100)
    },
    style() {
      return {
        width: this.scaleWidth + 'px',
        height: this.scaleHeight + 'px'
      }
    },
    lineHeight() {
      return this.scaleHeight + 'px'
    },
    lineRadius() {
      return this.scaleHeight / 2 + 'px'
    },
    buttonHeight() {
      return this.scaleHeight * 1.5 + 'px'
    },
    buttonTop() {
      return -this.scaleHeight * 0.25 + 'px'
    },
    buttonRadius() {
      return this.scaleHeight * 0.75 + 'px'
    }
  },
  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
         需要在父组件created中初始化！！ */
    this.registerToRefList()
    this.initEventHandler()

    this.handleOnCreated()
  },

  mounted() {
    this.handleOnMounted()
  },

  beforeUnmount() {
    this.unregisterFromRefList()
  },

  methods: {}
}
</script>

<style lang="scss" scoped>
.slider-field-editor :deep(.el-slider__runway) {
  background-color: v-bind('field.options.colorBack');
  height: v-bind('lineHeight');
  border-radius: v-bind('lineRadius');

  .el-slider__bar {
    background-color: v-bind('field.options.colorSelect');
    height: v-bind('lineHeight');
    border-radius: v-bind('lineRadius');
  }

  .el-slider__button-wrapper {
    height: v-bind('buttonHeight');
    width: v-bind('buttonHeight');
    top: v-bind('buttonTop');
    border-radius: v-bind('buttonRadius');
    z-index: unset;

    .el-slider__button {
      height: v-bind('buttonHeight');
      width: v-bind('buttonHeight');
      border-color: v-bind('field.options.colorSelect');
      background-color: v-bind('field.options.colorSelect');

      &:hover {
        cursor: default;
      }
    }
  }
}
</style>
